package org.jumiz.framework.base.support;

import org.jumiz.framework.base.reflect.BeanDefinition;

import java.util.HashMap;
import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * 扩展支持的类工厂
 * @author jumiz
 */
public class BeanFactory {

    private static HashMap<String, Object> interfaceImpl = new HashMap<>();

    static {
        ServiceLoader<FieldAnnotationSupport> load = ServiceLoader.load(FieldAnnotationSupport.class);
        Iterator<FieldAnnotationSupport> iterator = load.iterator();
        if (iterator.hasNext()) {
            interfaceImpl.put(FieldAnnotationSupport.class.getName(), iterator.next());
        } else {
            interfaceImpl.put(FieldAnnotationSupport.class.getName(), new DefaultFieldAnnotationSupport());
        }

        ServiceLoader<SqlBuilder> sqlBuilders = ServiceLoader.load(SqlBuilder.class);
        Iterator<SqlBuilder> sqlBuilderIterator = sqlBuilders.iterator();
        if (sqlBuilderIterator.hasNext()) {
            interfaceImpl.put(SqlBuilder.class.getName(), sqlBuilderIterator.next());
        } else {
            interfaceImpl.put(SqlBuilder.class.getName(), new DefaultSqlBuilder());
        }
    }

    public static <T> T getBean(Class<T> tClass) {
        return (T) interfaceImpl.get(tClass.getName());
    }
}
